package com.excesys.exsecs.energy.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.excesys.exsecs.common.core.util.R;
import com.excesys.exsecs.common.excel.annotation.RequestExcel;
import com.excesys.exsecs.common.excel.annotation.ResponseExcel;
import com.excesys.exsecs.common.log.annotation.SysLog;
import com.excesys.exsecs.common.security.annotation.Inner;
import com.excesys.exsecs.energy.entity.EnergyInput;
import com.excesys.exsecs.energy.service.EnergyInputService;
import com.excesys.exsecs.energy.vo.EnergyInputExceVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;

/**
 * 通用能耗流水表
 *
 * @author zhang
 * @date 2024-09-10 14:40:46
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/energyinput")
@Tag(description = "EnergyInput", name = "通用能耗流水表管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class EnergyInputController {

	private final EnergyInputService energyInputService;

	/**
	 * 分页查询
	 *
	 * @param page        分页对象
	 * @param energyInput 通用能耗流水表
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('bum_EnergyInput_view')")
	public R getEnergyInputPage(@ParameterObject Page page, @ParameterObject EnergyInput energyInput) {
		LambdaQueryWrapper<EnergyInput> wrapper = Wrappers.lambdaQuery();
		wrapper.eq(EnergyInput::getEnergyType, energyInput.getEnergyType());
		wrapper.like(StringUtils.isNotBlank(energyInput.getUnitName()), EnergyInput::getUnitName, energyInput.getUnitName());
		return R.ok(energyInputService.page(page, wrapper));
	}


	@Operation(summary = "手抄表能耗分析查询", description = "手抄表能耗分析查询")
	@GetMapping("/query")
	public R getEnergyInputQuery(@ParameterObject EnergyInput energyInput) {
		LambdaQueryWrapper<EnergyInput> wrapper = Wrappers.lambdaQuery();
		wrapper.eq(EnergyInput::getEnergyType, energyInput.getEnergyType());
		wrapper.groupBy(EnergyInput::getUnitNo);
		return R.ok(energyInputService.getBaseMapper().selectList(wrapper));
	}


	/**
	 * 通过id查询通用能耗流水表
	 *
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('bum_EnergyInput_view')")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(energyInputService.getById(id));
	}

	/**
	 * 新增通用能耗流水表
	 *
	 * @param energyInput 通用能耗流水表
	 * @return R
	 */
	@Operation(summary = "新增通用能耗流水表", description = "新增通用能耗流水表")
	@SysLog("新增通用能耗流水表")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('bum_EnergyInput_add')")
	public R save(@RequestBody EnergyInput energyInput) {
		return R.ok(energyInputService.save(energyInput));
	}

	/**
	 * 修改通用能耗流水表
	 *
	 * @param energyInput 通用能耗流水表
	 * @return R
	 */
	@Operation(summary = "修改通用能耗流水表", description = "修改通用能耗流水表")
	@SysLog("修改通用能耗流水表")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('bum_EnergyInput_edit')")
	public R updateById(@RequestBody EnergyInput energyInput) {
		return R.ok(energyInputService.updateById(energyInput));
	}

	/**
	 * 通过id删除通用能耗流水表
	 *
	 * @param ids id列表
	 * @return R
	 */
	@Operation(summary = "通过id删除通用能耗流水表", description = "通过id删除通用能耗流水表")
	@SysLog("通过id删除通用能耗流水表")
	@DeleteMapping
	@PreAuthorize("@pms.hasPermission('bum_EnergyInput_del')")
	public R removeById(@RequestBody Long[] ids) {
		return R.ok(energyInputService.removeBatchByIds(CollUtil.toList(ids)));
	}


	/**
	 * 导出excel 表格
	 *
	 * @param energyInput 查询条件
	 * @param ids         导出指定ID
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('bum_EnergyInput_export')")
	public List<EnergyInput> export(EnergyInput energyInput, Long[] ids) {
		return energyInputService.list(Wrappers.lambdaQuery(energyInput).in(ArrayUtil.isNotEmpty(ids), EnergyInput::getId, ids));
	}


	/**
	 * 导入excel 表格
	 *
	 * @return excel 文件流
	 */
	@PostMapping("/import")
	public R importEnergyInput(@RequestExcel List<EnergyInputExceVo> energyInputExceVos, BindingResult bindingResult) {
		return energyInputService.batchImport(energyInputExceVos, bindingResult);
	}


	/*
	 *手抄表能耗统计
	 * @param  unitNo 查询设备位置编码
	 * @param date 查询时间 yyyy、yyyy-MM、yyyy-MM-dd
	 * @param energyType 比较类型：0能耗 1同比，2环比
	 * */
	@Operation(summary = "手抄表能耗统计", description = "手抄表能耗统计")
	@GetMapping("/powerData")
	@Inner(value = false)
	public R powerData(String unitNo, String energyType) throws ParseException {
		return R.ok(energyInputService.powerData(unitNo, energyType), "手抄表能耗统计");
	}

	/*
	 *手抄表能耗柱状图
	 * @param  unitNo 查询设备位置编码
	 * @param date 查询时间 yyyy、yyyy-MM、yyyy-MM-dd
	 * @param energyType 比较类型：0能耗 1同比，2环比
	 * */
	@Operation(summary = "手抄表能耗柱状图", description = "手抄表能耗柱状图")
	@GetMapping("/powerLayer")
	@Inner(value = false)
	public R powerLayer(String unitNo, String energyType, String date) throws ParseException {
		return R.ok(energyInputService.powerLayer(unitNo, energyType, date), "手抄表能耗柱状图");
	}

	@Operation(summary = "手抄表能耗排序", description = "手抄表能耗排序")
	@GetMapping("/powerSort")
	@Inner(value = false)
	public R powerSort(String energyType, String date) throws ParseException {
		return R.ok(energyInputService.powerSort(energyType, date), "手抄表能耗柱状图");
	}



}